package com.unusap.remote.core.remote.hessian;

import org.springframework.remoting.caucho.HessianProxyFactoryBean;

import com.unusap.remote.core.remote.factory.AbstractHttpWebServiceFactory;
import com.unusap.remote.core.remote.factory.FactoryBeanWebServiceDirector;

/**
 * Created by IntelliJ IDEA.
 * User: danson
 * Date: 2009-8-20
 * Time: 16:56:34
 * To change this template use File | Settings | File Templates.
 */
public class AbstractHessianWebServiceFactory extends AbstractHttpWebServiceFactory {

    public AbstractHessianWebServiceFactory(String serviceHost) {
        super(serviceHost);
    }

    public <T> T getService(Class<T> serviceInterface, String serviceName) {
        return getHessianService(serviceInterface, serviceName);
    }

    @SuppressWarnings("unchecked")
	private <T> T getHessianService(Class<T> serviceInterface, String serviceName) {
        HessianProxyFactoryBean proxyFactoryBean = new HessianProxyFactoryBean();
        proxyFactoryBean.setServiceInterface(serviceInterface);
        proxyFactoryBean.setServiceUrl(constructServiceUrl(serviceName));
        proxyFactoryBean.setProxyFactory(createProxyFactory());
        return new FactoryBeanWebServiceDirector<HessianProxyFactoryBean>(proxyFactoryBean)
        		.getWebService();
    }

    private <T> HttpHeaderSupportHessianProxyFactory createProxyFactory() {
        HttpHeaderSupportHessianProxyFactory hessianProxyFactory = new HttpHeaderSupportHessianProxyFactory(httpHeadersProvider);
        hessianProxyFactory.setConnectTimeout(timeout);
        hessianProxyFactory.setUser(user);
        hessianProxyFactory.setPassword(password);
        return hessianProxyFactory;
    }

}
